#include "addrecord.h"

AddRecord::AddRecord()
{
    setupUi(this);
    connect(this, SIGNAL(on_addCategoryButton_clicked()), this, SLOT(addNewCategory()));
    connect(this, SIGNAL(on_deleteCategoryButton_clicked()), this, SLOT(deleteCategory()));
    connect(this,SIGNAL(on_recordAcceptButton_clicked()), this, SLOT(construct()));
    this->recordDateTime->setMaximumDateTime(QDateTime::currentDateTime());
    this->recordDateTime->setDateTime(QDateTime::currentDateTime());
    setWindowFlags(Qt::WindowTitleHint | Qt::WindowMinimizeButtonHint);
    layout() -> setSizeConstraint(QLayout::SetFixedSize);
}
void AddRecord::construct(){
    if(this->record_name->text().isEmpty())
        QMessageBox::warning(this,"Add Income","The record you are attempting to enter has no name.",
                             QMessageBox::Ok);
    else{
        sendRecord(this->recordDateTime->dateTime(),this->recordInfo->toPlainText().toStdString(),
                   this->record_name->text().toStdString(), this->categoryCombo->currentText().toStdString(), this->recordAmount->value());
        this->close();
    }
}
void AddRecord::addNewCategory(){
    int index = this->categoryCombo->findText(addCategory->text());
    if(!addCategory->text().isEmpty()){
        if(index != -1){
            QMessageBox::warning(this,"Add Category","The category you are attempting to enter already exists.",
                                 QMessageBox::Ok);
        }
        else
            sendCategory(addCategory->text().toStdString());
    }
    else{
        QMessageBox::warning(this,"Add Category","The category you are attempting to enter has no name.",
                             QMessageBox::Ok);
    }
}

void AddRecord::deleteCategory(){
    removeCategory(categoryCombo->currentText().toStdString());
}
